package com.orphan.marketFront.controller;


import com.orphan.marketFront.service.IShippingAddressService;
import com.orphan.marketFront.util.ResultCode;
import com.orphan.marketFront.util.ResultVO;
import com.orphan.util.dto.ShippingAddressDto;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("shippingAddress")
public class ShippingAddressController {

    @Autowired
    private IShippingAddressService service;

    @GetMapping("/selectShippingAddress")
    @ApiOperation("根据用户ID查询收货地址")
    public ResultVO selectShippingAddress(@ApiParam("用户ID") @RequestParam("userId") Integer userId){
        List<ShippingAddressDto> list = service.selectShippingAddress(userId);
        return ResultVO.success(list);
    }

    @PostMapping("/insertShippingAddress")
    @ApiOperation("新增收货地址")
    public ResultVO insertShippingAddress(@ApiParam("新增收货地址") @RequestBody ShippingAddressDto shippingAddressDto){
        Boolean result = service.insertShippingAddress(shippingAddressDto);
        if(result){
            return ResultVO.success(ResultCode.SUCCESS);
        }else {
            return ResultVO.success("新增失败");
        }
    }

    @DeleteMapping("/deleteShippingAddressById")
    @ApiOperation("删除收货地址")
    public ResultVO deleteShippingAddressById(@ApiParam("要删除的地址ID") @RequestParam("addressId") String addressId){
        Boolean result = service.deleteShippingAddressById(addressId);
        if(result){
            return ResultVO.success(ResultCode.SUCCESS);
        }else {
            return ResultVO.success("删除失败");
        }
    }

    @PutMapping("/updateShippingAddress")
    @ApiOperation("修改收货地址")
    public ResultVO updateShippingAddress(@ApiParam("修改收货地址") @RequestBody ShippingAddressDto shippingAddressDto){
        Boolean result = service.updateShippingAddress(shippingAddressDto);
        if(result){
            return ResultVO.success(ResultCode.SUCCESS);
        }else {
            return ResultVO.success("修改失败");
        }
    }

    @PutMapping("/updateDefaultAddress")
    @ApiOperation("修改默认地址")
    public ResultVO updateDefaultAddress(@ApiParam("新的默认地址") @RequestBody ShippingAddressDto shippingAddressDto){
        Boolean result = service.updateDefaultAddress(shippingAddressDto);
        if(result){
            return ResultVO.success(ResultCode.SUCCESS);
        }else {
            return ResultVO.success("修改失败");
        }
    }

}
